using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using DevExpress.XtraTab;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrinting.Control;
using DevExpress.XtraEditors;
using System.Drawing.Printing;
using ECRMS.WinForm.BaseClass;
using ECRMS.WinForm.BaseClass.Common;
using ECRMS.WinForm.Properties;
using ECRMS.WinForm.WinService;
using System.Text;
using DevExpress.XtraRichEdit;

namespace ECRMS.WinForm.Modules.ReportPrintControl
{

    public class MasterDetailControl1 : XtraUserControl {

        public Core core=Core.getInstance();

        PrintControl pc = new PrintControl();

        XtraTabControl PSTab;
        XtraTabPage tabPage1;
        System.Windows.Forms.ImageList imageList1;
        System.Windows.Forms.ImageList imageList2;
        System.ComponentModel.IContainer components;
        PrintingSystem printingSystem;

        public event EventHandler PrintControlChanged;

        public PrintControl PrintControl {
            get {
                XtraTabPage tp = PSTab.TabPages[PSTab.SelectedTabPageIndex];
                if(tp.Controls.Count > 0) {
                    PrintControl pc = tp.Controls[0] as PrintControl;
                    if(pc != null)
                        return pc;
                }
                return null;
            }
        }
        public PrintingSystem PrintingSystem {
            get { return printingSystem; }
            set {
                printingSystem = value;
                printingSystem.PageSettings.LeftMargin = 110;
                printingSystem.PageSettings.RightMargin = 110;
                CreateReport(printingSystem);
                pc.PrintingSystem = printingSystem;
                printingSystem.SetCommandVisibility(PrintingSystemCommand.ExportTxt, CommandVisibility.All);
                printingSystem.SetCommandVisibility(PrintingSystemCommand.ExportXls, CommandVisibility.All);
                printingSystem.SetCommandVisibility(PrintingSystemCommand.ExportXlsx, CommandVisibility.All);
                printingSystem.SetCommandVisibility(PrintingSystemCommand.ExportCsv, CommandVisibility.All);
                printingSystem.SetCommandVisibility(PrintingSystemCommand.ExportHtm, CommandVisibility.All);
                printingSystem.SetCommandVisibility(PrintingSystemCommand.ExportMht, CommandVisibility.All);
                printingSystem.SetCommandVisibility(PrintingSystemCommand.ExportRtf, CommandVisibility.All);

                printingSystem.SetCommandVisibility(PrintingSystemCommand.SendTxt, CommandVisibility.All);
                printingSystem.SetCommandVisibility(PrintingSystemCommand.SendXls, CommandVisibility.All);
                printingSystem.SetCommandVisibility(PrintingSystemCommand.SendXlsx, CommandVisibility.All);
                printingSystem.SetCommandVisibility(PrintingSystemCommand.SendCsv, CommandVisibility.All);
                printingSystem.SetCommandVisibility(PrintingSystemCommand.SendMht, CommandVisibility.All);
                printingSystem.SetCommandVisibility(PrintingSystemCommand.SendRtf, CommandVisibility.All);
            }
        }

        public MasterDetailControl1() {
            InitializeComponent();

            pc.Dock = DockStyle.Fill;
            tabPage1.Controls.Add((System.Windows.Forms.Control)pc);
        }

        #region Component Designer generated code
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MasterDetailControl1));
            this.PSTab = new DevExpress.XtraTab.XtraTabControl();
            this.tabPage1 = new DevExpress.XtraTab.XtraTabPage();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.imageList2 = new System.Windows.Forms.ImageList(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.PSTab)).BeginInit();
            this.PSTab.SuspendLayout();
            this.SuspendLayout();
            // 
            // PSTab
            // 
            this.PSTab.Dock = System.Windows.Forms.DockStyle.Fill;
            this.PSTab.Location = new System.Drawing.Point(0, 0);
            this.PSTab.Name = "PSTab";
            this.PSTab.SelectedTabPage = this.tabPage1;
            this.PSTab.Size = new System.Drawing.Size(150, 150);
            this.PSTab.TabIndex = 3;
            this.PSTab.TabPages.AddRange(new DevExpress.XtraTab.XtraTabPage[] {
            this.tabPage1});
            this.PSTab.SelectedPageChanged += new DevExpress.XtraTab.TabPageChangedEventHandler(this.PSTab_SelectedIndexChanged);
            // 
            // tabPage1
            // 
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Size = new System.Drawing.Size(143, 121);
            this.tabPage1.Text = "Main Report";
            // 
            // imageList1
            // 
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.TransparentColor = System.Drawing.Color.Magenta;
            this.imageList1.Images.SetKeyName(0, "Book_Closed_16x16.png");
            this.imageList1.Images.SetKeyName(1, "Book_Opened_16x16.png");
            // 
            // imageList2
            // 
            this.imageList2.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList2.ImageStream")));
            this.imageList2.TransparentColor = System.Drawing.Color.Magenta;
            this.imageList2.Images.SetKeyName(0, "Customers_icon.png");
            this.imageList2.Images.SetKeyName(1, "Order_Icon_64x64.png");
            // 
            // MasterDetailControl
            // 
            this.Controls.Add(this.PSTab);
            this.Name = "MasterDetailControl";
            ((System.ComponentModel.ISupportInitialize)(this.PSTab)).EndInit();
            this.PSTab.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        private void CreateReport(PrintingSystem ps)
        {
            BrickGraphics gr = ps.Graph;
            ps.Begin();
            gr.BorderWidth = 0;
            gr.Modifier = BrickModifier.Detail;
            
            FuncResult result = core.CoreReportOfDesign.GetReports(core.CoreUser.UserInfo.CorporationRefId);

            if (result.Status == 0)
            {
                XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                return;
            }

            StringBuilder builder = new StringBuilder();
            foreach (CtReport item in core.CoreReportOfDesign.Reports)
            {
                if (item.Content != null)
                {
                    builder.Append(System.Text.Encoding.UTF8.GetString(item.Content.ToArray()));
                }
            }
            //string aa = builder.ToString();
            string aa = core.CoreReportOfDesign.Reports[0].Content.ToString();
            gr.DrawString(aa, new RectangleF(0, 0, gr.ClientPageSize.Width, 2000));

            ps.End();
        }

        
        private void PSTab_SelectedIndexChanged(object sender, TabPageChangedEventArgs e) {
            OnPrintControlChanged();
        }
        void OnPrintControlChanged() {
            if(PrintControlChanged != null)
                PrintControlChanged(this, EventArgs.Empty);
        }
    }
}
